package com.Util;

import com.Bean.Message;
import net.sf.json.JSONObject;

import java.sql.*;
import java.util.HashSet;
import java.util.Set;

public class MessDao extends DB{

    public static Set<Message> selectMessBytkid(String tkid){
        Set<Message> messageSet= new HashSet<Message>();
        connector();
        try (Connection conn = DriverManager.getConnection(URL, USER, PWD)) {
            if (DB.isDebug()) {
                System.out.println("MessDao.getMess() processing");
            }
            Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);

            ResultSet rs = stmt.executeQuery("select * from "+DBMESS+" where tkid='" + tkid + "' order by posttime DESC ");

            int i=0;
            while(rs.next()) {
                Message message = new Message();
                    message.setTkid(rs.getString("tkid"));
                    message.setId(rs.getString("id"));
                    message.setUsername(rs.getString("username"));
                    //message.setPosttime(LocalDateTime.parse(rs.getString("posttime").replace(" ","T"));
                    message.setPosttime(rs.getString("posttime"));
                    message.setContent(rs.getString("content"));
                    messageSet.add(message);
                if (DB.isDebug()) {

                    JSONObject jb = JSONObject.fromObject(message);
                    String tojson = jb.toString();
                    System.out.println("message["+i+"] for select");
                    System.out.println(tojson);
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return messageSet;
    }

    /**
     * 传入一个Message对象，将其数据插入到数据库中
     * @param message
     * @return 运行状态值,1表示成功, 0表示失败
     */
    public static int updateMess(Message message){
        connector();
        try (Connection conn = DriverManager.getConnection(URL, USER, PWD)) {
            PreparedStatement stat = conn.prepareStatement("insert into `"+DBMESS+"` (`tkid`,`username`,`posttime`,`content`)" +
                    "values(?,?,now(),?)");
            if (DB.isDebug()) {
                System.out.println("MessDao.updateUser() processing");
                JSONObject jb = JSONObject.fromObject(message);
                String tojson = jb.toString();
                System.out.println("message for update");
                System.out.println(tojson);
            }

            stat.setInt(1, Integer.parseInt(message.getTkid()));
            stat.setString(2, message.getUsername());
            //java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            stat.setString(3, message.getContent());
            if (DB.isDebug()) {
                System.out.println("Complete stat.setString()");
                System.out.println(stat);
            }
            int result =  stat.executeUpdate();
            return result;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
